/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pricecomparison.websitereader.projectorquest;

import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JLabel;
import jxl.write.WriteException;
import pricecomparison.websitereader.WebsiteReader;

/**
 *
 * @author Jim Simon
 */
public class PQ_ItemListReader extends PQ_HomePageReader {
    String URL = super.URL;
    WebsiteReader reader;

    public PQ_ItemListReader(String url) throws java.net.MalformedURLException, java.io.IOException {
        URL = url;
        try {
            reader = new WebsiteReader(URL);
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }
    }

    public void beginSearch(JLabel l) throws jxl.write.WriteException, java.net.MalformedURLException, java.io.IOException {
        ArrayList<String> items = new ArrayList();
        // Get items
        try {
            items = getItems();
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }

        for (int i = 0; i < items.size(); i++) {
            if (reader.DEBUG)
                System.out.println( "-- Found " + items.get(i));

            l.setText(Integer.toString(Integer.parseInt(l.getText()) + 1));
            try {
                PQ_ItemReader item = new PQ_ItemReader(items.get(i));
                item.beginSearch();
            } catch (jxl.write.WriteException e) {
                throw e;
            } catch (java.net.MalformedURLException e) {
                throw e;
            } catch (java.io.IOException e) {
                throw e;
            }
        }
    }

    private ArrayList<String> getItems() throws java.net.MalformedURLException, java.io.IOException {
        ArrayList<String> links = new ArrayList();
        try {
            reader = new WebsiteReader(URL);
            
            String line = reader.readLine();
        
            while (line != null) {
                if(line.contains("<h2 class=\"product-name\">"))
                    links.add(extractLink(line));
                else if (line.contains("<div class=\"toolbar-bottom\">"))
                    break;
                line = reader.readLine();
            }

            // Close stream
            reader.close();
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }
        
        return links;
    }
    
     public String getNextPage() throws java.io.IOException {
         try {
            String line = reader.readLine();
            while (line != null) {
                if (line.contains("<a class=\"next\""))
                    return extractLink(line);
                else if (line.contains("<ul class=\"products-grid\">"))
                    break;

                line = reader.readLine();
            }
         } catch (java.io.IOException e) {
             throw e;
         }
        
        return "";
    }
     
    private String extractLink(String line) {
        // Find beginning
        String key = "href=\"";
        int pos = line.indexOf(key);
        
        // Consume upto line
        line = line.substring(pos + key.length());
        
        // Find end
        key = "\"";
        pos = line.indexOf(key);
        
        // Cut away end of line
        line = line.substring(0, pos);

        return line;
    }
}
